<?php
/***
* @version $Id: captcha_gd.php 336 2007-01-23 08:12:56Z flexiondotorg $
* @copyright (c) 2006 - 2007 Flexion.Org
*            (c) 2006              Horst Nogajski
*            (c) 2006              Paul Sohier
*            (c) 2005              Ruben Barkow, Thorsten Ventzke http://www.eclabs.de/
*            (c) 2004 - 2005 Project Minerva
*            (c) 2001 - 2006 phpBB Group
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
***/

if (!defined('IN_R3BORN'))
{
	die('Hacking attempt');
}

class captcha
{
	function execute($code)
	{
		global $config;

		$stats = gd_info();

		if (substr($stats['GD Version'], 0, 7) === 'bundled')
		{
			$bundled = true;
		}
		else
		{
			$bundled = false;
		}

		preg_match('/[\\d.]+/', $stats['GD Version'], $version);

		if (version_compare($version[0], '2.0', '>='))
		{
			$gd_version = 2;
		}
		else
		{
			$gd_version = 1;
		}

		// create the image, stay compat with older versions of GD
		if ($gd_version === 2)
		{
			$func1 = 'imagecreatetruecolor';
			//$func2 = 'imagecolorallocate';
		}
		else
		{
			$func1 = 'imagecreate';
			//$func2 = 'imagecolorclosest';
		}

		$fonts = $this->get_fonts();

		$width = rand(256,384);
		$height =  rand(64,112);

		//$image = imagecreatetruecolor($width,$height);
		$image = $func1($width, $height);
		$background = imagecolorallocate($image, $this->color('bg'), $this->color('bg'), $this->color('bg'));

		imagefilledrectangle($image, 0, 0, $width-1, $height-1, $background);

		if ($config['complex_confirm'])
		{
			// random characters in background with random position, angle, color
			for($g = 0;$g < 30; $g++)
			{
				$t = dss_rand();
				$t = $t[0];

				$ypos = rand(0,$height);
				$xpos = rand(0,$width);

				$color = imagecolorallocate($image, $this->color('bgtext'), $this->color('bgtext'), $this->color('bgtext'));

                // Display random lines/ellipses
				if (!($g % 2))
				{
					$scribx = rand(0,($width*2));
					$scriby = rand(0,($height*2));

					$wibblex = rand(0,($width*2));
					$wibbley = rand(0,($height*2));

					imageline($image, rand(0, $scribx-$xpos), rand(0, $scriby+$ypos), rand(0, $wibblex), rand(0, $wibbley), $color+$g);
					imageellipse($image, rand(0, $scribx+$xpos), rand(0, $scriby-$ypos), rand(0, $scriby), rand(0, $scribx), $color-$g);
				}

				imagettftext($image, $this->size(), $this->angle(), $xpos, $ypos, $color, $this->shuffle_fonts($fonts), $t);
			}
		}
		else
		{
			// generate grid
			for ($i = 0; $i < $width; $i += 13)
			{
				$color	= imagecolorallocate($image, $this->color('bggrid'), $this->color('bggrid'), $this->color('bggrid'));
				imageline($image, $i, 0, $i, $height, $color);
			}

			for ($i = 0; $i < $height; $i += 11)
			{
				$color	= imagecolorallocate($image, $this->color('bggrid'), $this->color('bggrid'), $this->color('bggrid'));
				imageline($image, 0, $i, $width, $i, $color);
			}
		}

		$stukje = $width / (strlen($code) + 3);

		for($j = 0;$j < strlen($code); $j++)
		{
			$tek = $code[$j];
			$ypos = rand(32,$height-8);
			$xpos = $stukje * ($j+2);

			$color2 = imagecolorallocate($image, $this->color('text'), $this->color('text'), $this->color('text'));

			imagettftext($image, $this->size(), $this->angle(), $xpos, $ypos, $color2, $this->shuffle_fonts($fonts), $tek);
		}

		// Output image
		header('Content-Type: image/png');
		header('Cache-control: no-cache, no-store');
		imagepng($image);
		imagedestroy($image);
	}


	/**
	* Function to create a random color
	* @auteur mastercode.nl
	* @param $type string Mode for the color
	* @return int
	**/
	function color($type)
	{
		switch($type)
		{
			case 'bg':
				$color = rand(224,255);
				break;
			case 'text':
				$color = rand(0,127);
				break;
			case 'bggrid':
			case 'bgtext':
				$color = rand(200,224);
				break;
			default:
				$color = rand(0,255);
				break;
		}
		return $color;
	}

	/**
	* Function to ranom the size
	* @auteur mastercode.nl
	* @return int
	**/
	function size()
	{
		$size = mt_rand(18,32);
		return $size;
	}
	/**
	* Function to random the posistion
	* @auteur mastercode.nl
	* @return int
	**/
	function angle()
	{
		$angle = mt_rand(-40,40);
		return $angle;
	}

	/**
	* Function to return a ttf file from fonts array
	* @auteur mastercode.nl
	* @return string
	**/
	function shuffle_fonts($ar)
	{
		global $root_path;

		if(count($ar))
		{
			shuffle($ar);
			$i = rand(0,(count($ar) - 1));
			return $root_path . 'includes/captcha/fonts/' . $ar[$i];
		}
	}

	/**
	* Function to return an arrary of ttf files
	* @auteur mastercode.nl
	* @return array
	**/
	function get_fonts()
	{
		global $root_path;

		$f = @opendir($root_path . 'includes/captcha/fonts/');
		$ar = array();
		while(($file = @readdir($f)) !== false)
		{
			if(!in_array($file,array('.','..')) && eregi('.ttf',$file))
			{
				$ar[] = $file;
			}
		}
		return $ar;
	}
}

?>